package com.fsy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fsy.entities.Book;
import com.fsy.entities.Member;
import com.fsy.entities.Member_excel;
import com.fsy.service.MemberService;
import com.fsy.utils.ExcelUtil;
import com.fsy.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/member")
public class MemberController {
    //注入
    @Resource
    private MemberService memberService;

    //按条件查询全部
    @GetMapping("/findByMember")
    public Map<String,Object> findByMember(Member member){
        //查询全部人员信息
        List<Member> memberList = memberService.findByMember(member);
        //查询全部人员数量
        int count = memberService.selectMemberCounts(member);
        return Result.toClient("0", "",count,memberList);
    }

    //查询院系
    @GetMapping("getDepartment")
    public List<Member> getDepartment(){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.select("DISTINCT department");
        return memberService.list(queryWrapper);
    }

    /**
     * 导出excel表
     * @param response
     * @param member
     */
    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, Member member){
        List<Member_excel> list = memberService.exportMember(member);
        ExcelUtil.export(response,"member","member",list,Member_excel.class);
    }
    //新增
    @PostMapping
    public Map<String,Object> add(@ModelAttribute Member member){
        boolean save = memberService.save(member);
        Map<String,Object> map = new HashMap<>();
        if (save){
            map.put("code","0");
            map.put("msg","新增成功");
        }else {
            map.put("code","1");
            map.put("msg","新增失败");
        }
        return map;
    }

    //修改
    @PutMapping
    public Map<String,Object> update(@ModelAttribute Member member){
        boolean update = memberService.updateById(member);
        Map<String,Object> map = new HashMap<>();
        if (update){
            map.put("code","0");
            map.put("msg","修改成功");
        }else {
            map.put("code","1");
            map.put("msg","修改失败");
        }
        return map;
    }

    //删除
    @DeleteMapping("/{id}")
    public Map<String,Object> del(@PathVariable("id") Integer id){
        boolean del = memberService.removeById(id);
        Map<String,Object> map = new HashMap<>();
        if (del){
            map.put("code","0");
            map.put("msg","删除成功");
        }else {
            map.put("code","1");
            map.put("msg","删除失败");
        }
        return map;
    }

    //删除
    @DeleteMapping("/MebDel")
    public Map<String,Object> batchDel(List<Integer> list){
        boolean del = memberService.removeBatchByIds(list);
        Map<String,Object> map = new HashMap<>();
        if (del){
            map.put("code","0");
            map.put("msg","删除成功");
        }else {
            map.put("code","1");
            map.put("msg","删除失败");
        }
        return map;
    }
}
